//
//  ViewController.swift
//  HZJChart
//
//  Created by HZJ on 17/1/13.
//  Copyright © 2017年 HZJ. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
//        var titleDataa = [NSString:NSNumber]()
//        print(titleDataa.count)
//        titleDataa = ["1":23,"2":78,"3":23]
////        print(titleDataa.count)
////        var s = (titleDataa as NSDictionary).allKeys as! [NSString]
////        print(s)
//        let v = HZJChartView(frame: CGRectMake(0,200,self.view.frame.width,400))
//        v.charData = titleDataa
//        self.view.addSubview(v)
//        v.setLineChart(true)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let v = FirstViewController()
        self.navigationController?.pushViewController(v, animated: true)
    }

}

class FirstViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.randomColor()
        
        
        let b = UIBarButtonItem(title:"back", style: UIBarButtonItemStyle.done, target: self, action: #selector(FirstViewController.clickLeftBth))
        
        let leftSpaceItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil)
        leftSpaceItem.width = 15
        navigationItem.leftBarButtonItems = [leftSpaceItem,b]
        
        
        var titleDataa = [NSString:NSNumber]()
        print(titleDataa.count)
        titleDataa = ["1":23,"2":78,"3":23]
        chartView = HZJChartView(frame: CGRect(x: 0,y: 200,width: self.view.frame.width,height: 400))
        chartView.charData = titleDataa
        chartView.distanceH = 15
        chartView.distanceV = 30
        self.view.addSubview(chartView)
        chartView.setLineChart(true)
        
//        let gesture = UISwipeGestureRecognizer(target: self, action: "swip:")
//        self.view.addGestureRecognizer(gesture)
        
    }
//    func swip(gesture:UISwipeGestureRecognizer){
//        print(gesture.direction)
//    }
    
    fileprivate var index:Int64 = 0
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        index += 1;
        chartView.setLineChart(index % 2 == 0 ? false : true)
    }
    fileprivate var chartView:HZJChartView!
    
    @objc func clickLeftBth() {
//        self.navigationController?.popToRootViewControllerAnimated(true)
//        let view = self.navigationController?.childViewControllers[0]
//        self.navigationController?.popToViewController(view!, animated: true)
        self.navigationController?.popViewController(animated: true)

    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}

class HZJNavigationController: UINavigationController,UIGestureRecognizerDelegate {
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        //如此做，当重写navigationBar右侧按钮后，若经push操作，仍可以用手势操作返回上一级
        self.interactivePopGestureRecognizer?.delegate = self
        super.pushViewController(viewController, animated: animated)
    }
    ///所有的手势返回上一级，皆会走此步
    override func popViewController(animated: Bool) -> UIViewController? {
        return super.popViewController(animated: animated)
    }
    ///navigation的手势操作（作用范围有限，一般为左侧一部分空间），不会影响其他控件的手势操作，
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        guard self.childViewControllers.count == 1 else {
            return true
        }
        guard gestureRecognizer.isKind(of: UIPanGestureRecognizer.self) else{
            return true
        }
        return false
    }
}
